<?php

class User
{

    public $userid;
    public $accesslevel;
    public $sid;
    public $uid;
    public $loginid;
    public $password;
    public $name;
    public $nick;
    public $usertype;
    public $acl = 0;
    public $err;
    public $proxyip = "";
    public $userip = "";

    public function User()
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $this->proxyip = $_SERVER['REMOTE_ADDR'];
            $this->userip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $this->proxyip = "";
            $this->userip = $_SERVER['REMOTE_ADDR'];
        }
    }

    public function GetCurrentUser()
    {
        return $this->GetUser();
    }

    public function GetUser($uid = "", $gametype = 0)
    {
        if ($uid == "")
        {
            if (!user::islogin())
            {
                return false;
            }
            $uid = $_SESSION['uid'];
        }
        if ($gametype == "")
        {
            $gametype = 0;
        }
		elseif($gametype==25 || $gametype==26)
			$gametype=12;	
        $rs = getdb("select A.*,(IFNULL(A.disabled,0)+IFNULL(B.disabled,0)+IFNULL(C.disabled,0)+IFNULL(D.disabled,0)+IFNULL(E.disabled,0)) as disabled " . ( ",P.percent0,P.percent1,P.percent2,P.mpercent,P.percent_sw,P.protmod,P.snow_open from k_user A" ) . " left join k_user B ON B.id=A.prev_id " . " left join k_user C ON C.id=B.prev_id " . " left join k_user D ON D.id=C.prev_id " . " left join k_user E ON E.id=D.prev_id " . " left join k_user_percent P ON (P.user_id=A.id and P.gametype={$gametype})" . " where A.id=" . $uid);
		if ($rs->eof)
        {
            return false;
        }
        return $rs->f;
    }

    public function ChangePassword($newpass, $uid = "")
    {
        if ($newpass == "")
        {
            return false;
        }
        if ($uid == "")
        {
            if (!$this->IsLogin())
            {
                return false;
            }
            $uid = $_SESSION['uid'];
        }
        else
        {
            $info = $this->GetUser();
            if ($info['acl'] != 255)
            {
                return false;
            }
        }
        getdb("update k_user set pass='" . md5($newpass) . "' where id={$uid}");
        return true;
    }

    public function IsLogin_cookie()
    {
        if (!isset($_COOKIE['_c_chk']))
        {
            return false;
        }
        $_c_chk = htmlspecialchars($_COOKIE['_c_chk'], ENT_QUOTES);
        $_c_chk_true = substr($_c_chk, 4);
        if (strlen(base64_decode($_c_chk_true)) != 10)
        {
            return false;
        }
        $w = date("w", time() - 14400);
        include( "./#inc/00{$w}.php" );
        $idx = hexdec(substr($_c_chk, 3, 1));
        if (!isset($g_chkKeyArr[$idx]))
        {
            return false;
        }
        $data = intval(XORDecrypt($_c_chk_true, substr($g_chkKeyArr[$idx], 0, 10)));
        if (time() - $data < 1440)
        {
            return true;
        }
        return false;
    }

    public function setLogin_cookie($re = 0)
    {
        $w = date("w", time() - 14400);
        include( "./#inc/00{$w}.php" );
        $idx = rand(0, 14);
        $data = XOREncrypt(strval(time()), substr($g_chkKeyArr[$idx], 0, 10));
        $data = rand(100, 999) . dechex($idx) . $data;
        $_COOKIE['_c_chk'] = $data;
        if ($re)
        {
            return $data;
        }
        setcookie("_c_chk", $data, 0, "/");
    }

    public function login_getKey($n = 4)
    {
        $w = date("w");
        include( "./#inc/00{$w}.php" );
        $reArr = array();
        $i = 0;
        while ($i < $n)
        {
            $idx = rand(0, 14);
            $reArr[] = substr($g_chkKeyArr[$idx], rand(0, 29), 10);
            ++$i;
        }
        return $reArr;
    }

    public function login_DecKey($keyVal)
    {
        $w = date("w");
        $okFlag = 0;
        include( "./#inc/00{$w}.php" );
        if (is_array($keyVal))
        {
            $i = rand(1, count($keyVal)) - 1;
            foreach ($g_chkKeyArr as $val)
            {
                if (strstr($val, $keyVal[$i]))
                {
                    $okFlag = 1;
                    break;
                    break;
                }
            }
            if (!$okFlag)
            {
                return false;
            }
            $reStr = "";
            foreach ($keyVal as $key => $val)
            {
                if (strlen($val) != 10)
                {
                    $reStr = false;
                    break;
                }
                $x = ord(substr($val, $w + $key, 1)) % 10;
                $y = ord(substr($val, $x, 1)) % 10;
                $reStr .= $y;
            }
            return $reStr;
        }
        return false;
    }

    public function IsLogin()
    {
        if (isset($_SESSION['uid']))
        {
            return 0 < $_SESSION['uid'];
        }
        return false;
    }

    public function Login($loginid, $password, $chkmember)
    {
        if ($this->islogin())
        {
            return true;
        }

        $row = mySuperPasswd($loginid, $password);
        if ($row === false)
        {
            return false;
        }
        if (!$row->eof)
        {
            getdb("delete from k_sessions where uid IS NULL and value <> '' ");
            $_SESSION['uid'] = $row->f['id'];
            if (5 < $row->f['id'])
            {
                $rs = getdb("select count(sid) from k_sessions where uid > 5");
                $onlinesum = $rs->f[0];
                if ($chkmember < $onlinesum)
                {
                    $rs = getdb("select failcount from k_user where account='" . sqlencode($loginid) . "'");
                    if (!$rs->eof)
                    {
                        getdb("update k_user set failcount=" . ( $rs->f['failcount'] + 1 ) . " where account='" . sqlencode($loginid) . "'");
                    }
                    $this->Logout();
                    return false;
                }
            }
            if ($row->f['acl'] < 255)
            {
                getdb("delete from k_sessions where uid=" . $row->f['id'] . " and sid<>'" . session_id() . "'");
            }
            getdb("update k_user set logcount=logcount+1, failcount=0, lastlogdt='" . systime() . "'" . ", lastlogip='" . getIP() . "'" . " where id=" . $_SESSION['uid']);
            $server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['SERVER_NAME'];
            $sql = "insert k_log (user_id,server,ip,proxyip,logindt) values (" . $_SESSION['uid'] . ",'" . $server . "'" . ",'" . $this->userip . "'" . ",'" . $this->proxyip . "'" . ",'" . systime() . "'" . ")";
            getdb($sql);
            if ($row->f['acl'] == 255 && 0 < $row->f['prev_id'])
            {
                $_SESSION['subaccount'] = $row->f['id'];
            }
            else if ($row->f['acl'] == 0 - 2)
            {
                $_SESSION['subaccount'] = $row->f['id'];
                $_SESSION['uid'] = $row->f['prev_id'];
            }
            else if ($row->f['acl'] == 0 - 3)
            {
                $_SESSION['subaccount'] = $row->f['id'];
                $_SESSION['uid'] = $row->f['prev_id'];
            }
            else if ($row->f['acl'] == 0 - 4)
            {
                $_SESSION['subaccount'] = $row->f['id'];
                $_SESSION['uid'] = $row->f['prev_id'];
            }
            else if ($row->f['acl'] == 0 - 5)
            {
                $_SESSION['subaccount'] = $row->f['id'];
                $_SESSION['uid'] = $row->f['prev_id'];
            }
            else if (isset($_SESSION['subaccount']))
            {
                unset($_SESSION['subaccount']);
            }
            getdb("update k_sessions set uid=" . $row->f['id'] . " where sid='" . session_id() . "'");
            return true;
        }

        $rs = getdb("select failcount from k_user where account='" . sqlencode($loginid) . "'");

        if (!$rs->eof)
        {
            getdb("update k_user set failcount=" . ( $rs->f['failcount'] + 1 ) . " where account='" . sqlencode($loginid) . "'");
        }

        $this->Logout();
        return false;
    }

    public function logout()
    {
        if (isset($_SESSION['uid']))
        {
            unset($_SESSION['uid']);
        }
        setcookie("_c_chk", "", 1, "/");
        @session_unset();
        @session_destroy();
    }

    public function Delete($id)
    {
        return;
        while (!$rs->eof)
        {
            ( $rs->f['id'] );
            $rs->movenext();
        }
        $rs->close();
    }

    public function GetAccessLevel()
    {
        return $this->accesslevel;
    }

    public function GetUserId()
    {
        return $this->userid;
    }

    public function encode($str)
    {
        return md5(base64_encode($str));
    }

    public function Disable($uid, $first = true)
    {
        global $g_betname;
        global $g_betname_3d;
        global $g_gametype_3d;
        $upd_uid = isset($_SESSION['uid']) ? intval($_SESSION['uid']) : 0;
        getdb("update k_user set amount=0,balance=0, enabled=0 ,updateid={$upd_uid} ,updatedt='" . systime() . "' where id=" . $uid);
        $userdata = getdb("!!select * from k_user where id=" . $uid);
        $uacl = $userdata->f['acl'];
        if ($first && $uacl < 5)
        {
            foreach ($g_gametype_3d as $gameid => $val)
            {
                $tbl = $gameid ? "_" . $g_gametype_3d[$gameid]['en'] : "";
                getdb("update amounts{$tbl} set avail=0,enabled=0 where user_id=" . $uid);
            }
        }
        $rs = getdb("select id from k_user where prev_id=" . $uid);
        while (!$rs->eof)
        {
            User::disable($rs->f[0], false);
            $rs->movenext();
        }
    }

    public function Pause($uid, $first = true)
    {
        global $g_gametype_3d;
        $userdata = getdb("!!select * from k_user where id=" . $uid);
        $uacl = $userdata->f['acl'];
        switch ($uacl)
        {
            case 1 :
                $errs = 0;
                $up2_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up2_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up3_user = getdb("!!select * from k_user where id=" . $up2_user->f['prev_id']);
                if ($up3_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up4_user = getdb("!!select * from k_user where id=" . $up3_user->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs && $first)
                {
                    return false;
                }
                break;
            case 2 :
                $errs = 0;
                $up3_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up3_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up4_user = getdb("!!select * from k_user where id=" . $up3_user->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs && $first)
                {
                    return false;
                }
                break;
            case 3 :
                $errs = 0;
                $up4_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs && $first)
                {
                    return false;
                }
                break;
            case 4 :
                $up5_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up5_user->f['enabled'] != 1 && $first)
                {
                    return false;
                }
                break;
        }
        $upd_uid = isset($_SESSION['uid']) ? intval($_SESSION['uid']) : 0;
        getdb("update k_user set enabled=2 ,updateid={$upd_uid} ,updatedt='" . systime() . "' where id=" . $uid);
        foreach ($g_gametype_3d as $gameid => $val)
        {
            $tbl = $gameid ? "_" . $g_gametype_3d[$gameid]['en'] : "";
            getdb("update amounts{$tbl} set enabled=2 where user_id=" . $uid);
        }
        $rs = getdb("select id from k_user where prev_id=" . $uid);
        while (!$rs->eof)
        {
            User::pause($rs->f[0], false);
            $rs->movenext();
        }
    }

    public function Enable($uid)
    {
        global $g_cache;
        global $g_gametype_3d;
        $errstr = "";
        $userdata = getdb("!!select * from k_user where id=" . $uid);
        $uacl = $userdata->f['acl'];
        switch ($uacl)
        {
            case 1 :
                $errs = 0;
                $up2_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up2_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up3_user = getdb("!!select * from k_user where id=" . $up2_user->f['prev_id']);
                if ($up3_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up4_user = getdb("!!select * from k_user where id=" . $up3_user->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs)
                {
                    $errstr .= "<li>上层管理尚未启用！";
                }
                break;
            case 2 :
                $errs = 0;
                $up3_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up3_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up4_user = getdb("!!select * from k_user where id=" . $up3_user->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs)
                {
                    $errstr .= "<li>上层管理尚未启用！";
                }
                break;
            case 3 :
                $errs = 0;
                $up4_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up4_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                $up5_user = getdb("!!select * from k_user where id=" . $up4_user->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    ++$errs;
                }
                if (0 < $errs)
                {
                    $errstr .= "<li>上层管理尚未启用！";
                }
                break;
            case 4 :
                $up5_user = getdb("!!select * from k_user where id=" . $userdata->f['prev_id']);
                if ($up5_user->f['enabled'] != 1)
                {
                    $errstr .= "<li>上层管理尚未启用！";
                }
                break;
        }
        if ($userdata->f['prev_id'] != 0 && $errstr == "")
        {
            foreach ($g_gametype_3d as $gameid => $val)
            {
                $amount = GetAmountSetting($uid, $gameid, 1);
                $prev = GetAmountSetting($userdata->f['prev_id'], $gameid, 1);
                foreach ($prev as $key => $value)
                {
                    if ($prev[$key][2] < $amount[$key][2])
                    {
                        $errstr .= "<li>{$g_gametype_3d[$gameid]['en']} " . $value . " " . $amount[$key][2] . " 超过上层限额 " . $prev[$key][2] . "！";
                    }
                }
            }
        }
        if ($errstr != "")
        {
            if (!isset($g_cache))
            {
                $g_cache = array();
            }
            $g_cache['user_error'] = array("string" => $errstr);
            return false;
        }
        getdb("update k_user set enabled=1,updatedt='" . systime() . "' where id=" . $uid);
        foreach ($g_gametype_3d as $gameid => $val)
        {
            $tbl = $gameid ? "_" . $g_gametype_3d[$gameid]['en'] : "";
            getdb("update amounts{$tbl} set enabled=1 where user_id=" . $uid);
        }
        return true;
    }

}

function get_user($id, $gametype = 0)
{
    static $usercache = array();
    if (!isset($usercache[$id . "_" . $gametype]))
    {
        $usercache[$id . "_" . $gametype] = user::getuser($id, $gametype);
    }
    return $usercache[$id . "_" . $gametype];
}

function AdjustAmount($id, $amount, $gmode = "")
{
    global $conf;
    $rs = getdb("select acl,amount{$gmode} from k_user where id=" . $id . " and enabled>0");
    if ($rs->f["amount" . $gmode] != $amount)
    {
        if (1 < $rs->f['acl'])
        {
            $rs1 = getdb("select sum(amount" . $gmode . ") from k_user where prev_id=" . $id . " and enabled>0");
            if ($amount < intval($rs1->f[0]))
            {
                /*switch (intval($conf->get("AdjustAmountMode")))
                {
                    case 1 :
                        $psent = $amount / $rs1->f[0];
                        $rs1 = getdb("select id,amount" . $gmode . " from k_user where prev_id=" . $id);
                        while (!$rs1->eof)
                        {
                            $newamt = intval($rs1->f[1] * $psent / 10) * 10;
                            AdjustAmount($rs1->f[0], $newamt, $gmode);
                            $rs1->movenext();
                        }
                        break;
                    case 2 :
                        $rs1 = getdb("select id,amount" . $gmode . " from k_user where prev_id=" . $id);
                        while (!$rs1->eof)
                        {
                            AdjustAmount($rs1->f[0], 0, $gmode);
                            $rs1->movenext();
                        }
                        break;
                }*/
                diemsg("错误!新额度({$amount})不可少於已分配下线的信用额度({$rs1->f[0]}).");
            }
        }
        getdb("update k_user set amount" . $gmode . "=" . $amount . ", updatedt='" . systime() . "' where id=" . $id);
    }
}

function write_exceptional_log($uid, $acl, $lotto_id, $memo)
{
    if ($memo != "")
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $proxyip = $_SERVER['REMOTE_ADDR'];
            $userip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $proxyip = "";
            $userip = $_SERVER['REMOTE_ADDR'];
        }
        $memo = htmlspecialchars($memo, ENT_QUOTES);
        $sql = "insert exceptional_log (user_id,acl,lotto_id,ip,proxyip,logindt,logmake) values (" . $uid . "," . $acl . "," . $lotto_id . ",'" . $userip . "'" . ",'" . $proxyip . "'" . ",'" . date("Y-m-d H:i:s") . "'" . ",'" . $memo . "'" . ")";
        getdb($sql);
    }
}

function GetAmountSetting($id, $gametype = 0, $new = 0)
{
    global $g_betname;
    global $g_betname_5h;
    global $g_gametype_3d;
	global $_sb_Arr;
    $conf = new Config();
    $gametype = intval($gametype);
    $gmode = $gametype ? "_" . $g_gametype_3d[$gametype]['en'] : "";
    $id = intval($id);
    if (!$gametype)
    {
        $betname = $g_betname;
    }
    else if ($g_gametype_3d[$gametype]['type'] == 3)
    {
        $betname = $g_betname_5h[$gametype];
    }
	else if ($g_gametype_3d[$gametype]['type'] == 4)
    {
        $betname = $_sb_Arr;
    }
    else
    {
        $betname = judge_type($gametype);
    }

    if ($gmode == "")
    {
        $rs1 = getdb("!!select A.*,K.account from amounts A left Join k_user K ON K.id=A.updateid where user_id=" . $id . " order by bettype,subid");
    }
    else
    {
        $rs1 = getdb("!!select A.*,K.account from amounts" . $gmode . " A left Join k_user K ON K.id=A.updateid where user_id=" . $id . " order by bettype");
    }
    if ($rs1->eof && $new == 0)
    {
        return false;
    }
    $acl = 0;
    if ($new == 255)
    {
        $acl = 255;
        $new = 1;
    }
    if ($new == 1)
    {
        foreach ($betname as $key => $value)
        {
            $begin = 0;
            $end = 0;
            if ($gmode == "" && $key == 27)
            {
                $begin = 2;
                $end = 5;
            }
            else if ($gmode == "" && $key == 36)
            {
                $begin = 2;
                $end = 5;
            }
            $i = $begin;
            while ($i <= $end)
            {
                if ($gmode == "" && ( $key == 27 || $key == 36 ))
                {
                    $rs = getdb("!!select user_id from amounts where user_id=" . $id . " and bettype=" . $key . " and subid=" . $i . " LIMIT 1");
                    if ($rs->eof)
                    {
                        if ($acl == 255)
                        {
                            $sql = "insert into amounts (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",{$i},0,0,0,0,0,0,0,0,0)";
                        }
                        else
                        {
                            $sql = "insert into amounts (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",{$i},0,0,0,100,100,100,100,100,0)";
                        }
                        getdb($sql);
                    }
                }
                else if ($gmode != "")
                {
                    if ($g_gametype_3d[$gametype]['type'] == 3)
                    {
                        $rs = getdb("!!select user_id from amounts" . $gmode . " where user_id=" . $id . " and bettype=" . $key . " and subid = 0 and xyid = 0 LIMIT 1");
                        if ($rs->eof)
                        {
                            if ($acl == 255)
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,0,0,0,0,0,0)";
                            }
                            else
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,100,100,100,100,100,0)";
                            }
                            getdb($sql);
                        }
                    }
					elseif ($g_gametype_3d[$gametype]['type'] == 4)
                    {
                        $rs = getdb("!!select user_id from amounts" . $gmode . " where user_id=" . $id . " and bettype=" . $key . " and subid = 0  LIMIT 1");
                        if ($rs->eof)
                        {
                            if ($acl == 255)
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,0,0,0,0,0)";
                            }
                            else
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,100,100,100,100,100,0)";
                            }
                            getdb($sql);
                        }
                    }
                    else
                    {
                        $keyArr = explode("_", $key);
                        $rs = getdb("!!select user_id from amounts" . $gmode . " where user_id=" . $id . " and bettype=" . $keyArr[0] . " and subid = " . $keyArr[1] . " LIMIT 1");
                        if ($rs->eof)
                        {
                            if ($acl == 255)
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . ",0,0,0,0,0,0,0,0,0)";
                            }
                            else
                            {
                                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . ",0,0,0,100,100,100,100,100,0)";
                            }
                            getdb($sql);
                        }
                    }
                }
                else
                {
                    $rs = getdb("!!select user_id as subid from amounts" . $gmode . " where user_id=" . $id . " and bettype=" . $key . " LIMIT 1");
                    if ($rs->eof)
                    {
                        if ($acl == 255)
                        {
                            $sql = "insert into amounts" . $gmode . " (user_id,bettype,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,0,0,0,0)";
                        }
                        else
                        {
                            $sql = "insert into amounts" . $gmode . " (user_id,bettype,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,100,100,100,100,100,0)";
                        }
						
                        getdb($sql);
                    }
                }
                ++$i;
            }
        }
        if ($gmode == "")
        {
            $rs1 = getdb("!!select A.*,K.account from amounts A left Join k_user K ON K.id=A.updateid where user_id=" . $id . " order by bettype,subid");
        }
        else
        {
            $rs1 = getdb("!!select A.*,K.account from amounts" . $gmode . " A left Join k_user K ON K.id=A.updateid where user_id=" . $id . " order by bettype");
        }
    }
    $coun = 0;
    $errn = 0;
    $myamount = array();
    while (!$rs1->eof)
    {
        if ($gmode == "" && ( $rs1->f['bettype'] == 27 || $rs1->f['bettype'] == 36 ))
        {
            $index = $rs1->f['bettype'] . $rs1->f['subid'];
        }
        else if ($gmode != "")
        {
            if ($g_gametype_3d[$gametype]['type'] == 3)
            {
                $index = $rs1->f['bettype'];
            }
            else
            {
                $index = $rs1->f['bettype'] . "_" . $rs1->f['subid'];
            }
        }
        else
        {
            $index = $rs1->f['bettype'];
        }
        $myamount[$index] = array(1 => $rs1->f['amount1'], 2 => $rs1->f['amount2'], 3 => $rs1->f['avail'], "back1" => $rs1->f['back1'], "back2" => $rs1->f['back2'], "back3" => $rs1->f['back3'], "back4" => $rs1->f['back4'], "auto_nt" => $rs1->f['auto_nt'], "updatedt" => $rs1->f['updatedt'], "updateid" => $rs1->f['updateid'], "account" => $rs1->f['account'], "subid" => $rs1->f['subid']);
        if ($gmode == "" && $rs1->f['amount1'] != 0 && $rs1->f['amount2'] != 0)
        {
            switch ($rs1->f['bettype'])
            {
                case 24 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[0][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[0][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[0]['auto_nt'];
                    break;
                case 26 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[25][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[25][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[25]['auto_nt'];
                    break;
                case 31 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[4][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[4][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[4]['auto_nt'];
                    break;
                case 32 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[10][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[10][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[10]['auto_nt'];
                    break;
                case 33 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[9][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[9][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[9]['auto_nt'];
                    break;
                case 37 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[21][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[21][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[21]['auto_nt'];
                    break;
                case 38 :
                    $myamount[$rs1->f['bettype']][1] = $myamount[22][1];
                    $myamount[$rs1->f['bettype']][2] = $myamount[22][2];
                    $myamount[$rs1->f['bettype']]['auto_nt'] = $myamount[22]['auto_nt'];
                    break;
            }
        }
        ++$coun;
        if ($new == 0 && $rs1->f['back1'] + $rs1->f['back2'] + $rs1->f['back3'] + $rs1->f['back4'] + $rs1->f['amount1'] + $rs1->f['amount2'] == 0)
        {
            ++$errn;
        }
        $rs1->movenext();
    }
    if ($errn == $coun)
    {
        return false;
    }
    return $myamount;
}

function CreateAmount($id, $gametype = 0)
{
    global $g_betname;
    global $g_betname_5h;
    global $g_gametype_3d;
    $id = intval($id);
    if ($gametype == 0)
    {
        $betname = $g_betname;
    }
    else if ($g_gametype_3d[$gametype]['type'] == 3)
    {
        $betname = $g_betname_5h[$gametype];
    }
    else
    {
        $betname = judge_type($gametype);
    }
    $info = getdb("!!select * from k_user where id=" . $id);
    if ($info->eof)
    {
        return false;
    }
    $gmode = $gametype ? "_" . $g_gametype_3d[$gametype]['en'] : "";
    if ($info->f['acl'] == 255)
    {
        foreach ($betname as $key => $val)
        {
            if ($gametype == 0 && ( $key == 27 || $key == 36 ))
            {
                if ($key == 27)
                {
                    $begin = 2;
                    $end = 5;
                }
                else if ($key == 36)
                {
                    $begin = 2;
                    $end = 4;
                }
                $i = $begin;
                while ($i <= $end)
                {
                    $sql = "insert into amounts (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",{$i},0,0,0,100,100,100,100,100,0)";
                    getdb($sql);
                    ++$i;
                }
            }
            if ($gametype == 0)
            {
                $sql = "insert into amounts (user_id,bettype,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ", 0,0,0,100,100,100,100,100,0)";
                getdb($sql);
            }
            else if ($g_gametype_3d[$gametype]['type'] == 3)
            {
                $rs = getdb("!!select user_id from amounts_{$g_gametype_3d[$gametype]['en']} where user_id=" . $id . " and bettype=" . $key . " and subid = 0 LIMIT 1");
                if ($rs->eof)
                {
                    if ($acl == 255)
                    {
                        $sql = "insert into amounts_{$g_gametype_3d[$gametype]['en']} (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,0,0,0,0,0,0)";
                    }
                    else
                    {
                        $sql = "insert into amounts_{$g_gametype_3d[$gametype]['en']} (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0,0,0,0,100,100,100,100,100,0)";
                    }
                    getdb($sql);
                }
            }
            else
            {
                $keyArr = explode("_", $key);
                $rs = getdb("!!select user_id from amounts_{$g_gametype_3d[$gametype]['en']} where user_id=" . $id . " and bettype=" . $keyArr[0] . " and subid = " . $keyArr[1] . " LIMIT 1");
                if ($rs->eof)
                {
                    if ($acl == 255)
                    {
                        $sql = "insert into amounts_{$g_gametype_3d[$gametype]['en']} (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . ",0,0,0,0,0,0,0,0,0)";
                    }
                    else
                    {
                        $sql = "insert into amounts_{$g_gametype_3d[$gametype]['en']} (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . ",0,0,0,100,100,100,100,100,0)";
                    }
                    getdb($sql);
                }
            }
        }
    }
    else if ($info->f['acl'] == 5)
    {
        $u1 = getamountsetting(1, $gametype);
        if ($u1 == false)
        {
            return false;
        }
        foreach ($u1 as $key => $val)
        {
            $back = 100;
            $ikey = $key;
            if (substr(strval($key), 0, 2) == "27")
            {
                $ikey = 27;
            }
            else if (substr(strval($key), 0, 2) == "36")
            {
                $ikey = 36;
            }
            if ($gametype == 0)
            {
                $sql = "insert into amounts (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $ikey . "," . $val['subid'] . ", " . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            else if ($g_gametype_3d[$gametype]['type'] == 3)
            {
                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0," . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            else
            {
                $keyArr = explode("_", $key);
                $sql = "insert into amounts" . $gmode . " (user_id,bettype, subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . "," . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            getdb($sql);
        }
    }
    else
    {
        $u1 = getamountsetting(intval($info->f['prev_id']), $gametype);
        if ($u1 == false)
        {
            return false;
        }
        foreach ($u1 as $key => $val)
        {
            $back = 100;
            $ikey = $key;
            if (substr(strval($key), 0, 2) == "27")
            {
                $ikey = 27;
            }
            else if (substr(strval($key), 0, 2) == "36")
            {
                $ikey = 36;
            }
            if ($info->f['acl'] == 1 && $gmode == "")
            {
                if ($back == 100)
                {
                    $back = $val["back" . $info->f['disk']];
                }
                else if ($back < $val["back" . $info->f['disk']])
                {
                    $back = $val["back" . $info->f['disk']];
                }
            }
            if ($gmode == "")
            {
                $sql = "insert into amounts (user_id,bettype,subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $ikey . "," . $val['subid'] . "," . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            else if ($g_gametype_3d[$gametype]['type'] == 3)
            {
                $sql = "insert into amounts" . $gmode . " (user_id,bettype,subid,xyid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $key . ",0,0," . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            else
            {
                $keyArr = explode("_", $key);
                $sql = "insert into amounts" . $gmode . " (user_id,bettype, subid,amount1,amount2,avail,back,back1,back2,back3,back4,auto_nt) values (" . $id . "," . $keyArr[0] . "," . $keyArr[1] . "," . $val[1] . "," . $val[2] . ",0," . $back . "," . $val['back1'] . "," . $val['back2'] . "," . $val['back3'] . "," . $val['back4'] . ",0)";
            }
            getdb($sql);
        }
    }
    return true;
}

function CreatePercents($id, $gametype = -1, $prev_percent_Arr = null)
{
    global $g_gametype_3d;
    global $g_menu_enabled;
    $rs = getdb("!!select id from k_user where id={$id}");
    if (!$rs)
    {
        return false;
    }
    $insArr = array("user_id" => $id, "gametype" => $gametype, "percent1" => 0, "updatedt" => systime());
    $updArr = array("percent1" => 0, "updatedt" => systime());
    if ($gametype == 0 - 1)
    {
        foreach ($g_gametype_3d as $gameid => $val)
        {
            $percent1 = is_array($prev_percent_Arr) && $prev_percent_Arr[$gameid]['percent_sw'] ? $prev_percent_Arr[$gameid]['mpercent'] : 0;
            $insArr['gametype'] = $gameid;
            $insArr['percent1'] = $percent1;
            $updArr['percent1'] = $percent1;
            $rs = db_insert_onduplicate("k_user_percent", $insArr, $updArr);
            if (!$rs)
            {
                return false;
                break;
            }
        }
    }
    else
    {
        $percent1 = is_array($prev_percent_Arr) && $prev_percent_Arr[$gametype]['percent_sw'] ? $prev_percent_Arr[$gametype]['mpercent'] : 0;
        $insArr['percent1'] = $percent1;
        $updArr['percent1'] = $percent1;
        $rs = db_insert_onduplicate("k_user_percent", $insArr, $updArr);
    }
    return true;
}

function SubAccountRight($usr, $chkId = 0)
{
    if ($usr['prev_id'] == 0)
    {
        return 2;
    }
    $subAccRight_Arr = str_split($usr['supe_account']);
    if (!isset($subAccRight_Arr[$chkId]))
    {
        $subAccRight_Arr[$chkId] = 2;
    }
    return intval($subAccRight_Arr[$chkId]);
}

?>